//
//  PhotoBrowserDismissAnimation.m
//  XinLang_OC
//
//  Created by Boss Lin on 16/6/14.
//  Copyright © 2016年 cheuhui. All rights reserved.
//

#import "PhotoBrowserDismissAnimation.h"
#import "PhotoBrowserVC.h"

@implementation PhotoBrowserDismissAnimation

#pragma mark - UIViewControllerAnimatedTransitioning 代理

- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext {
    return 0.25;
}

- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext {
    // 获取到modal的控制器
    PhotoBrowserVC *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    
    // 获取modal出来的控制器的view
    UIView *fromView = [transitionContext viewForKey:UITransitionContextFromViewKey];
    
    // 获取到关闭时的过渡视图
    UIView *tempImageView = [fromVC dismissTempImageView];
    
    // 添加到容器视图
    [[transitionContext containerView] addSubview:tempImageView];
    
    // 隐藏collectionView
    fromVC.collectionView.hidden = YES;
    
    // 淡出动画
    [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
        // 缩放到缩略图的位置
        tempImageView.frame = [fromVC dismissTargetRect];
        fromView.alpha = 0;
    } completion:^(BOOL finished) {
        [transitionContext completeTransition:YES];
    }];
}


@end
